<template>
  <div class="login">
    <!-- <van-nav-bar>
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar> -->
    <navBar path="/home/find"></navBar>
    <div class="content">
      <h3 class="title">您好，请登录</h3>
      <van-form ref="form">
        <van-field
          placeholder="请输入手机号"
          v-model="form.mobile"
          :rules="rules.mobile"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont pr20">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          placeholder="请输入验证码"
          v-model="form.code"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont pr20">&#xe60c;</i>
          </template>
          <template #button>
            <span class="button" v-if="totalTime === 6" @click="getCode"
              >获取验证码</span
            >
            <span class="button" v-else>{{ totalTime }}秒</span>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意<span>《用户使用协议》</span>和<span>《隐私协议》</span>
      </div>
      <van-button type="danger" block round @click="submit">确定</van-button>
    </div>
  </div>
</template>

<script>
import { auCode, auLogin } from '@/api/login.js'
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      totalTime: 6,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator (value) {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请输入正确手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    if (getLocal()) {
      this.$router.push('/home')
    }
  },
  methods: {
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 验证码倒计时,这里的函数要用箭头函数，不能使用function，this的原因
          this.totalTime--
          const _timerId = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              // 当totalTime为0的时候关闭定时器
              clearInterval(_timerId)
              // 将totalTime从新赋值为初始值、
              this.totalTime = 6
            }
          }, 1000)
          this.$toast({
            type: 'loading',
            message: '加载中...',
            duration: 0
          })
          auCode({ mobile: this.form.mobile }).then(res => {
            this.$toast({
              type: 'success',
              message: res.data.data
            })
          })
        })
        .catch(() => {
          this.$toast({
            type: 'fail',
            message: '验证失败'
          })
        })
    },
    // 登录
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          auLogin(this.form)
            .then(res => {
              // 每次都写if else太麻烦，所以到响应拦截器中统一做处理
              // if (res.data.code === 200) {
              // console.log(res)
              // window.localStorage.setItem('token', res.data.data.jwt)
              // } else {
              //   this.$toast.fail(res.data.message)
              // }
              setLocal(res.data.data.jwt)
              this.$store.commit('getUserInfo', res.data.data.user)
              // 登录成功，将isLogin赋值为true
              this.$store.commit('setIsLogin', true)
              // 登录成功后跳转，1、创业面，2、配路由，3、跳转
              const _redirect = this.$route.query.redirect
              if (_redirect && _redirect !== '/login') {
                this.$router.push(_redirect)
              } else {
                this.$router.push('/home')
              }
            })
            .catch(err => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$toast({
            type: 'fail',
            message: '验证失败'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  .f44 {
    font-size: 44px;
  }
  // ::v-deep.van-nav-bar__left,
  // ::v-deep.van-nav-bar__right {
  //   padding: 0;
  // }
  .content {
    padding: @p15;
    .title {
      padding: 50px 0 63px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .pr20 {
      padding-right: 20px;
    }
    .button {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      padding: 15px 0 44px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      span {
        color: #00b8d4;
      }
    }
  }
}
</style>
